package com;

import com.aspose.pdf.internal.doc.ml.WnumberFormatValue;

import java.math.BigDecimal;

public class NumberToChinese {
    private static final String[] NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] UNITS = {"", "拾", "佰", "仟"};
    private static final String[] BIG_UNITS = {"", "万", "亿", "兆"};

    public String numberToChinese(String input) {
        double number = Double.parseDouble(input);

        boolean isNegative = number < 0;
        number = Math.abs(number);

        BigDecimal bigDecimal = new BigDecimal(String.valueOf(number));
        long integerPart = bigDecimal.longValue();
        int decimalPart = (bigDecimal.subtract(new BigDecimal(integerPart)).multiply(new BigDecimal(100))).intValue();

        if (integerPart == 0 && decimalPart == 0) {
            return isNegative ? "负零元整" : "零元整";
        }

        StringBuilder result = new StringBuilder();
        int unitIndex = 0;
        boolean zeroFlag = false;
        while (integerPart > 0) {
            long group = integerPart % 10000;
            if (group != 0) {
                if (zeroFlag) {
                    result.insert(0, "零");
                }
                result.insert(0, BIG_UNITS[unitIndex]);
                result.insert(0, groupToChinese(group));
                zeroFlag = false;
            } else {
                zeroFlag = true;
            }
            integerPart /= 10000;
            unitIndex++;
        }

        // 去除多余的零
        while (result.length() > 0 && result.charAt(0) == '零') {
            result.deleteCharAt(0);
        }

        // 如果小数部分为 0，直接添加“元整”
        if (decimalPart == 0) {
            // 避免出现“零元整”情况，这里判断结果最后是否是大单位，如果是直接加“整”
            if (result.length() > 0 && isBigUnit(result.charAt(result.length() - 1))) {
                result.append("整");
            } else if ("零".equals(result.substring(result.length() - 1, result.length()))) {
                result.replace(result.length() - 1, result.length(), "").append("元整");
            } else {
                result.append("元整");
            }
        } else {
            result.append("元");
            String decimalStr = decimalToChinese(decimalPart);
            if (!decimalStr.isEmpty()) {
                result.append(decimalStr);
            }
        }


        if (isNegative) {
            result.insert(0, "负");
        }

        return result.toString();
    }

    private static String groupToChinese(long group) {
        StringBuilder groupResult = new StringBuilder();
        boolean zeroFlag = false;
        for (int i = 0; i < 4; i++) {
            int digit = (int) (group / (long) Math.pow(10, 3 - i) % 10);
            if (digit != 0) {
                if (zeroFlag) {
                    groupResult.append("零");
                }
                groupResult.append(NUMBERS[digit]).append(UNITS[3 - i]);
                zeroFlag = false;
            } else {
                zeroFlag = true;
            }
        }
        String result = groupResult.toString();
        if (result.endsWith("零")) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    private static String decimalToChinese(int decimalPart) {
        StringBuilder decimalResult = new StringBuilder();
        int jiao = decimalPart / 10;
        int fen = decimalPart % 10;

        if (jiao != 0) {
            decimalResult.append(NUMBERS[jiao]).append("角");
        } else if (fen != 0) {
            decimalResult.append("零");
        }

        if (fen != 0) {
            decimalResult.append(NUMBERS[fen]).append("分");
        }

        return decimalResult.toString();
    }

    private static boolean isBigUnit(char c) {
        for (String unit : BIG_UNITS) {
            if (unit.length() > 0 && unit.charAt(0) == c) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        NumberToChinese nb = new NumberToChinese();
        String numList[] = {
                "701928.61",
                "584940.51",
                "935904.82",
                "116988.1"

        };

        for (String num : numList) {
            String positiveIntegerChinese = nb.numberToChinese(num);
            System.out.println(num + "  --->   " + positiveIntegerChinese);
        }
    }

}


